﻿package src.view.ui 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import src.event.menuEvent;
	import com.greensock.TweenLite;
	
	/**
	 * 顶部通栏，其中按钮可以弹出下拉菜单
	 * @author	MeatHill
	 * @version	0.1(2009-07-30)
	 */
	public class topBarView extends Sprite
	{
		
		public function topBarView() 
		{
			addEventListener(Event.ADDED, init);
		}
		
		/****************
		 * functions
		 * *************/
		private function init(evt:Event):void {
			removeEventListener(Event.ADDED, init);
			draw_bg();
			stage.addEventListener(Event.RESIZE, init);
		}
		private function draw_bg(evt:Event = null):void { 
			graphics.beginFill(0xDBE0E8);
			graphics.drawRect(0, 0, stage.stageWidth, 5);
			graphics.endFill();
		}
		private function menu_click_handler(evt:menuEvent):void {
      hide_menu_handler(evt);
			dispatchEvent(evt);
		}
		private function show_menu_handler(evt:menuEvent = null):void { 
			var _menu:topBarMenuView = topBarMenuView(getChildAt(getChildIndex(evt.currentTarget) - 1));
			TweenLite.to(_menu, .4, { show_height:_menu.total_height } );
		}
		private function hide_menu_handler(evt:menuEvent = null):void { 
      if (evt.currentTarget is topBarMenuView) {
        var _menu:Object = evt.currentTarget;
      } else {
        var _menu:Object = getChildAt(getChildIndex(evt.currentTarget) - 1);
      }
			TweenLite.to(_menu, .4, { show_height:0 } );
		}
		
		/***************
		 * methods
		 * ************/
		public function add_pro_title(pro_name:String, menu:XMLList):void { ;
			// 计算出来最左边的位置和间距
			var _distance:uint = uint(stage.stageWidth / (numChildren + 1));
			var _left_edge:uint = uint(_distance - proTitleView.WIDTH) / 2;
			
			// 将现有的通通左移
			for (var i:uint = 0; i < numChildren; i++) {
				getChildAt(i).x = _left_edge + _distance * i;
			}
			
			var _btn:proTitleView = new proTitleView(pro_name);
			_btn.x = _left_edge + _distance * numChildren;
			_btn.y = 2;
			_btn.addEventListener(menuEvent.SHOW_MENU, show_menu_handler);
			_btn.addEventListener(menuEvent.HIDE_MENU, hide_menu_handler);
			addChild(_btn);
			var _menu:topBarMenuView = new topBarMenuView(menu);
			_menu.x = _btn.x + 20;
			_menu.y = _btn.y + _btn.height;
			_menu.show_height = 0;
			_menu.addEventListener(menuEvent.SHOW_DETAIL, menu_click_handler);
			addChildAt(_menu, getChildIndex(_btn));
		}
	}
}